﻿using System;
using System.Net;
using BingoCommonConfig.Db;
using BingoCommonConfig.Db.Pool;
using BingoCommonConfig.Log4netConfig;
using BingoRedis;
using BingoServerCommon.Model;
using HPSocketCS;
using System.Threading;

namespace BingoGameServerConsole
{
	class Program
	{
		/// <summary>
		/// 服务器通用的连接池对象
		/// </summary>
		public static ConnectionPool ConnectionPool = null;

		/// <summary>
		/// redis里面的key
		/// </summary>
		public static string GameServerConfigKey = "gameServerConfig";

		/// <summary>
		/// Main
		/// </summary>
		/// <param name="args"></param>
		static void Main(string[] args)
		{
			try
			{
				Console.ForegroundColor = ConsoleColor.Red;
				Console.WriteLine("   Game Server 启动中 。。。。。");
				Console.ResetColor();

                //本机端口
                ushort port = 10001;


                

                var gameServer = new GameTcpServer();
                gameServer.Start(port);

				if (gameServer.Server.State == ServiceState.Started)
				{
					Console.WriteLine($"服务器启动成功  \t port:{port}");
				}

				

				#region 注册各个业务

				//注册所有的业务的各自的packages
				PackageRegister.PackagesRegister();

				#endregion


				#region mysql连接池

				//建立mysql连接池
				/*ConnectionPool = MysqlInit.Init();
				if (ConnectionPool == null)
				{
					Console.ForegroundColor = ConsoleColor.Red;
					Console.WriteLine("连接池初始化出错！");
					Console.ResetColor();
					return;
				}*/
                //TestConnectionPool t = new TestConnectionPool();
                //t.Test();

                #endregion

                #region 初始化房间

                //初始化房间管理器
                RoomManager.Instanse.StartWork(1);
                #endregion


                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine("停止服务器请输入q");
                Console.ResetColor();
                string strLine = "";
                do
                {
                    strLine = Console.ReadLine();
                    if (strLine != null)
                    {
                        switch (strLine.ToLower())
                        {
                            case "q":
                                gameServer.Server.Stop();
                                Console.WriteLine("关闭SOCKET成功");
                                Thread.Sleep(3000);
                                Environment.Exit(0);
                                break;
                            case "s":
                                //gameServer.BroadCast("");
                                Console.WriteLine("群发消息到所有客户端成功");
                                break;
                        }

                    }
                   

                }
                while (true);
            }
			catch (Exception e)
			{
				LogHelper.WriteFatal("Program.Main", e);
				throw;
			}




		}
	}
}
